<?php
include_once 'connection/connection_database.php';
class users{
    function check_user($name){
        $sql = "SELECT user_name FROM users WHERE user_name = '" . $name . "' ";
        $query = mysql_query($sql) or die(mysql_error());
        $data = mysql_num_rows($query);
        return $data;
    }
    
    function check_email($email){
        $sql = "SELECT user_email FROM users WHERE user_email = '" . $email . "' ";
        $query = mysql_query($sql) or die(mysql_error());
        $data = mysql_num_rows($query);
        return $data;
    }
    
    function check_pass($pass){
        $password = md5($pass["oldpass"]);
        $sql = "SELECT * FROM users WHERE user_id = '{$pass["id"]}' and user_pass = '{$password}'";
        $query = mysql_query($sql) or die(mysql_error());
        $data = mysql_num_rows($query);
        return $data;
    }
    
    function insert($user){
        $date = date("Y-m-d");
        $password = md5($user["password"]);
        $level = '{"create" : "'.$user["create"].'", "update" : "'.$user["update"].'", "delete" : "'.$user["delete"].'"}';
        $sql = "INSERT INTO users(user_name, user_pass, user_fullname, user_email, user_phone, user_registered, level) ";
        $sql .= "VALUES('".$user["username"]."', '".$password."', '".$user["fullname"]."', '".$user["email"]."', '".$user["phone"]."', '".$date."', '".$level."')";
        $query = mysql_query($sql) or die(mysql_error());
        return $query;
    }
    
    function InsertAdmin($user){
        $date = date("Y-m-d");
        $password = md5($user["password"]);
        $level = '{"admin":"admin"}';
        $sql = "INSERT INTO users(user_name, user_pass, user_fullname, user_email, user_phone, user_registered, level) ";
        $sql .= "VALUES('".$user["username"]."', '".$password."', '".$user["fullname"]."', '".$user["email"]."', '".$user["phone"]."', '".$date."', '".$level."')";
        $query = mysql_query($sql) or die(mysql_error());
        return $query;
    }
    
    function update($user){
        $level = '{"create" : "'.$user["create"].'", "update" : "'.$user["update"].'", "delete" : "'.$user["delete"].'"}';
        $sql = "UPDATE users SET user_fullname='{$user["fullname"]}', user_email='{$user["email"]}', user_phone='{$user["phone"]}', level='{$level}' WHERE user_id='{$user["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function delete($user){
        $sql = "DELETE FROM users WHERE user_id = {$user["id"]}";
        $query = mysql_query($sql) or die(mysql_error());
        return $query;
    }
    
    function select(){
        $sql = "SELECT * FROM users";
        $query = mysql_query($sql) or die(mysql_error());
        return $query;
    }
    
    function fill_one($user){
        $sql = "SELECT * FROM users WHERE user_id={$user["id"]}";
        $query = mysql_query($sql) or die(mysql_error());
        return mysql_fetch_array($query);
    }
    
    function FillOneByName($user){
        $sql = "SELECT * FROM users WHERE user_name='{$user["username"]}'";
        $query = mysql_query($sql) or die(mysql_error());
        return mysql_fetch_array($query);
    }
    
    function UpdateByName($user, $session){
        $sql = "UPDATE users SET user_fullname='{$user["fullname"]}', user_email='{$user["email"]}', user_phone='{$user["phone"]}' WHERE user_name='{$session["username"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function ChangePassword($user){
        $password = md5($user["newpass"]);
        $sql = "UPDATE users SET user_pass='{$password}' WHERE user_id='{$user["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function FillOneByLevel(){
        $user_level = '{"admin":"admin"}';
        $sql = "SELECT * FROM users WHERE level='{$user_level}'";
        $result = mysql_query($sql) or die(mysql_error());
        return mysql_fetch_array($result);
    }
}
?>
